package com.hsd.survey.action;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hsd.commons.model.HsdAccount;
import com.hsd.core.base.action.BaseAction;
import com.hsd.core.page.entity.PageParams;
import com.hsd.core.page.entity.Pagination;
import com.hsd.survey.SurveyConstants;
import com.hsd.survey.helper.SurveyModelHelper;
import com.hsd.survey.model.HsdModelSurvey;
import com.hsd.survey.service.ISurveyCommonService;
import com.hsd.survey.service.ISurveyDesignService;

/**
 * <p>
 * 类描述：问卷模板设计
 * <p>
 * 创建人：Abdi
 * <p>
 * 创建时间：Oct 3, 2013 11:19:55 AM
 */
@Controller
@RequestMapping("/model")
public class SurveyDesignAction extends BaseAction{
	@Autowired
	private ISurveyDesignService designService;
	
	@Autowired
	private ISurveyCommonService commonService;
	
	/**
	 * 我的问卷库：加载问卷模板列表
	 * @param pageParams
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/loadSurveyList.do")
	public String loadSurveyList(PageParams pageParams,HttpServletRequest request,ModelMap model){
		String name = request.getParameter("name");
		Pagination page = commonService.loadSurveyList(pageParams,name,String.valueOf(SurveyConstants.DESIGN_PUBLISH_COLLECT));

		model.addAttribute("page",page);
		model.addAttribute("name",name);
		return "app/survey/design/surveylib/query-survey-list";
	}
	
	/**
	 * 问卷设计：加载问卷模板列表
	 * @param pageParams
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/loadDesignList.do")
	public String loadDesignList(PageParams pageParams,HttpServletRequest request,ModelMap model){
		String name = request.getParameter("name");
		String status = request.getParameter("status");
		Pagination page = commonService.loadSurveyList(pageParams,name,status);

		model.addAttribute("page",page);
		model.addAttribute("name",name);
		return "app/survey/design/model/design-survey-list";
	}
	
	/**
	 * 问卷设计：新建问卷
	 * @param request
	 * @param model
	 * @return
	 */
	
	@RequestMapping(value="/createModelSurvey.do",produces = {"application/json;charset=UTF-8"})
	public @ResponseBody String createNewSurvey(HttpServletRequest request,ModelMap model){
		String name = request.getParameter("name");
		String createType = request.getParameter("createtype"); // 问卷创建方式 
		String type = request.getParameter("type"); // 问卷类型
		String isopen = request.getParameter("isopen"); // 问卷类型
		HsdAccount accInfo = this.getCurrentuser(request);
		
		HsdModelSurvey modelSurvey = SurveyModelHelper.initModelSurvey(name,createType,type,isopen,accInfo); //初始化问卷模板
		designService.save(modelSurvey); // 保存问卷
		return modelSurvey.getSurveyuuid();
	}
	
	/**
	 * 问卷设计：进入问卷设计页面
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/designSurvey.do",produces = {"application/json;charset=UTF-8"})
	public String designSurvey(HttpServletRequest request,ModelMap model){
		String surveyuuid = request.getParameter("surveyuuid");
		model.addAttribute("surveyuuid",surveyuuid);
		return "app/survey/design/model/design-survey-main";
	}
	
	/**
	 * 问卷设计：获取问卷的信息
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/getSurveyInfo.do",produces = {"application/json;charset=UTF-8"})
	public @ResponseBody String getSurveyInfo(HttpServletRequest request,ModelMap model){
		String surveyuuid = request.getParameter("surveyuuid");
		String surveyInfo = designService.getSurveyInfo(surveyuuid);
		System.out.println("===========surveyInfo " + surveyInfo);
		return surveyInfo;
	}
	
	/**
	 * 问卷设计：更新、保存问卷模板
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/updateSurvey.do")
	public @ResponseBody String updateSurvey(HttpServletRequest request,ModelMap model){
		String pageList = request.getParameter("pagelist");
		String questionList = request.getParameter("questionlist");
		String survey = request.getParameter("survey");
		System.out.println("===>>>> " + pageList);
		System.out.println("===>>>> " + questionList);
		System.out.println("===>>>> " + survey);
		
		designService.updateSurveyInfo(survey, pageList, questionList);
		
		String surveyuuid = request.getParameter("surveyuuid");
		String surveyInfo = designService.getSurveyInfo(surveyuuid);
		return surveyInfo;
	}
	
	/**
	 * 问卷设计：更新问卷状态
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/updateSurveyStatus.do",produces = {"application/json;charset=UTF-8"})
	public @ResponseBody String updateSurveyStatus(HttpServletRequest request,ModelMap model){
		String surveyuuid = request.getParameter("surveyuuid");
		String status = request.getParameter("status");
		designService.updateSurveyStatus(surveyuuid,status);
		return "操作成功！";
	}
}
